#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# zhouyi 6098550@qq.com

import json

class MiFlaskConfig:
    # port = '7777'
    def Show(self):
        print "flask.port:%d"%self.port

class MiMachineConfig:
    # method = 'rtu'
    # port = 'COM5'
    # stopbits = 1
    # bytesize = 8
    # parity = 'E'
    # baudrate = 19200
    # timeout = 1
    def Show(self):
        print "machine.method:%s"%self.method
        print "machine.port:%s" % self.port
        print "machine.stopbits:%d" % self.stopbits
        print "machine.bytesize:%s" % self.bytesize
        print "machine.parity:%s" % self.parity
        print "machine.baudrate:%s" % self.baudrate
        print "machine.timeout:%s" % self.timeout


class MiConfig(MiFlaskConfig):

    def Load(self, file_name):
        f = file(file_name)
        obj = json.load(f)
        self.machine_config = MiMachineConfig()
        self.flask_config   = MiFlaskConfig()

        self.machine_config.method   = obj["plc"]["method"]
        self.machine_config.port     = obj["plc"]["port"]
        self.machine_config.stopbits = obj["plc"]["stopbits"]
        self.machine_config.bytesize = obj["plc"]["bytesize"]
        self.machine_config.parity   = obj["plc"]["parity"]
        self.machine_config.baudrate = obj["plc"]["baudrate"]
        self.machine_config.timeout  = obj["plc"]["timeout"]
        self.flask_config.port       = obj['flask']['port']

        f.close()

    def Show(self):
        print "------------------------"
        self.flask_config.Show()
        print "------------------------"
        self.machine_config.Show()
        print "------------------------"

########################################################################################################################
## for test
#config = MiConfig()
#config.Load('./config.json')
#config.Show()